#ifndef PICTUREITEM_H
#define PICTUREITEM_H

#include <QtDeclarative/QDeclarativeItem>
#include <QDir>

class DrawThread;

const int Margin = 10;
const QString APP_NAME("Group My Pictures");
const QString APP_NAME_SHORT("gmp");
const QString EMPTYIMAGE_PATH(QDir::currentPath()+"/."+APP_NAME_SHORT+"/");
//const QImage::Format IMAGE_FORMAT(QImage::Format_ARGB32);
const QImage::Format IMAGE_FORMAT(QImage::Format_RGB32);

class PictureItem : public QDeclarativeItem
{
    Q_OBJECT
    Q_DISABLE_COPY(PictureItem)
    Q_ENUMS(ImageTypes)
    Q_ENUMS(ImageSizes)
    Q_ENUMS(DriveStorage)
    Q_PROPERTY(QUrl emptyImage READ emptyImage)
    Q_PROPERTY(QColor background READ background WRITE setBackground)
    Q_PROPERTY(QString freeFilename READ freeFilename)
    Q_PROPERTY(ImageTypes imageType READ imageType WRITE setImageType NOTIFY imageTypeChanged)
    Q_PROPERTY(DriveStorage storage READ storage WRITE setStorage NOTIFY storageChanged)
    Q_PROPERTY(QUrl createdImageName READ createdImageName)
    Q_PROPERTY(int aspectRatio READ aspectRatio WRITE setAspectRatio NOTIFY aspectRatioChanged)
    Q_PROPERTY(int margin READ margin WRITE setMargin NOTIFY marginChanged)

public:
    explicit PictureItem(QDeclarativeItem *parent = 0);
    ~PictureItem();

    enum ImageTypes {
        Nothing,
        Tile2by2,
        Side,
        UpDown,
        TwoUpOneDown,
        TwoLeftOneRight
    };

    enum ImageSizes {
        SizeNothing,
        QVGA,
        VGA,
        SVGA,
        XGA,
        WXGA
    };    

    enum DriveStorage {
        StorageInternal = 0,
        StorageRemovable,
        Gallery,
        StorageError
    };

    Q_INVOKABLE void createImage(ImageSizes imageSize, QVariantList imageList, QString fileName, QColor background, int margin, int aspectRatio);

    Q_INVOKABLE QStringList resolutionAsString(ImageSizes imageSize);

    Q_INVOKABLE QString imageSizeAsString(ImageSizes imageSize);

    Q_INVOKABLE QString imageSizeWithResolutionAsString(ImageSizes imageSize);

    Q_INVOKABLE void cancelDrawer();

    QUrl emptyImage();

    QString freeFilename();

    void setBackground(QColor backgroundColor);

    QColor background(){return m_background;}

    ImageTypes imageType() const {return m_imageType;}

    void setImageType(ImageTypes imageType){m_imageTypeOld = m_imageType; m_imageType = imageType; emit imageTypeChanged(m_imageType);}

    DriveStorage storage();
    void setStorage(DriveStorage storage);

    QUrl createdImageName(){return m_currentImage;}

    void setAspectRatio(int ratio){ m_aspect = ratio; emit aspectRatioChanged(m_aspect);}
    int aspectRatio(){return m_aspect;}

    void setMargin(int margin){m_margin = margin; emit marginChanged(m_margin);}
    int margin(){return m_margin;}

signals:
    void imageFinished(bool success);
    void imageTypeChanged(ImageTypes imageType);
    void canceled();
    void storageChanged(DriveStorage storage);
    void aspectRatioChanged(int ratio);
    void marginChanged(int margin);

protected:
    void paintEvent(QPaintEvent *event);

private slots:
    void drawFinished();    

private:
    QMap<ImageSizes,QStringList >   m_resolutions;
    ImageTypes                      m_imageType;
    ImageTypes                      m_imageTypeOld;
    ImageSizes                      m_imageSize;
    QVariantList                    m_imageList;
    QString                         m_fileName;
    QColor                          m_background;
    QColor                          m_backgroundOld;
    int                             m_margin;
    QString                         m_emptyName;
    QString                         m_picturesHome;
    DrawThread*                     m_thread;
    DriveStorage                    m_storage;
    QUrl                            m_currentImage;
    int                             m_aspect;
};

QML_DECLARE_TYPE(PictureItem)

#endif // PICTUREITEM_H

